package fr.waitandsee.fragments;

import fr.waitandsee.FragmentController;
import fr.waitandsee.R;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TextView;

public class ContactPickerFragment extends Fragment implements PageFragmentModel {
	
	private static final String TAG = "ContactPickerFragment";
	
	private EditText itFindSomebody;
	private ImageView ivSettings;
	private TableLayout tlSendInvitation;
	private TableLayout tlAcceptInvitation;
	private TableLayout tlSendMediaTo;
	private TableLayout tlUnlock;
	private TextView tvBack;
	private TextView tvSend;
	
	private FragmentController fragmentController;
	private Handler handler;
	
	public static ContactPickerFragment newInstance(/*parameters*/) {
		ContactPickerFragment fragment = new ContactPickerFragment();
		// set parameters if needed : 
	    // Bundle bundle = new Bundle(1);
	    // bundle.putInt(EXTRA_NAME, param);
	    // fragment.setArguments(bundle);
	    return fragment ;
	}
	
	
	  /////////////////////////
	 // FRAGMENT LIFE CYCLE //
	/////////////////////////
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate.savedInstanceState="+savedInstanceState);
		this.fragmentController = (FragmentController) getActivity();
		this.handler = new Handler();
	}
	
 	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 		Log.d(TAG, "onCreateView.savedInstanceState="+savedInstanceState);
		if (savedInstanceState != null) {
			// get saved parameters if needed : 
			// param = savedInstanceState.getInt(EXTRA_NAME);
		} else {
			if(getArguments() != null) {
				// get parameters if needed :
				// param = getArguments().getInt(EXTRA_NAME);
			}
		}
		View v = inflater.inflate(R.layout.fragment_contact_picker, container, false);
		return v;
	}
	
 	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
 		Log.d(TAG, "onViewCreated.savedInstanceState="+savedInstanceState);
		fetchViews();
		customizeViews();
		setListeners();
	}
 	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		Log.d(TAG, "onSaveInstanceState.outState="+outState);
		super.onSaveInstanceState(outState);
		// save parameters if needed :
		// outState.putInt(EXTRA_NAME, param);
	}
 	
	@Override
 	public void fetchViews() {
		this.itFindSomebody = (EditText) getView().findViewById(R.id.editText_find_somebody);
		this.ivSettings = (ImageView) getView().findViewById(R.id.imageView_head_settings);
		this.tlSendInvitation = (TableLayout) getView().findViewById(R.id.tableLayout_send_invitation);
		this.tlAcceptInvitation = (TableLayout) getView().findViewById(R.id.tableLayout_accept_invitation);
		this.tlSendMediaTo = (TableLayout) getView().findViewById(R.id.tableLayout_send_media_to);
		this.tlUnlock = (TableLayout) getView().findViewById(R.id.tableLayout_unlock);
		this.tvBack = (TextView) getView().findViewById(R.id.textView_back);
		this.tvSend = (TextView) getView().findViewById(R.id.textView_send);
 	}
	
	@Override
	public void customizeViews() {
		tlSendInvitation.setScrollContainer(false);
		tlAcceptInvitation.setScrollContainer(false);
		tlSendMediaTo.setScrollContainer(false);
		tlUnlock.setScrollContainer(false);
		
		//TODO add rows for "send invitation" table view
		// simulate rows in "send invitation" table :
		tlSendInvitation.addView(createRowForContact(new ContactRow(123154, "Anthony", false))); 	//DEBUG
		tlSendInvitation.addView(createSeparator());												//DEBUG
		tlSendInvitation.addView(createRowForContact(new ContactRow(465541, "Pauline", false)));	//DEBUG
		
		//TODO add rows for "accept invitation" table view
		// simulate rows in "accept invitation" table :
		tlAcceptInvitation.addView(createRowForContact(new ContactRow(346545, "Emma", false))); 	//DEBUG
		tlAcceptInvitation.addView(createSeparator());												//DEBUG
		tlAcceptInvitation.addView(createRowForContact(new ContactRow(254862, "Julie", false)));	//DEBUG
		
		//TODO add rows for "send media to" table view
		// simulate rows in "send media to" table :
		tlSendMediaTo.addView(createRowForContact(new ContactRow(354311, "Paul", false))); 	//DEBUG
		tlSendMediaTo.addView(createSeparator());											//DEBUG
		tlSendMediaTo.addView(createRowForContact(new ContactRow(114557, "Manon", false)));	//DEBUG
		
		//TODO add rows for "unlock" table view
		// simulate rows in "unlock" table :
		tlUnlock.addView(createRowForContact(new ContactRow(354311, "Camille", false))); 	//DEBUG
		tlUnlock.addView(createSeparator());												//DEBUG
		tlUnlock.addView(createRowForContact(new ContactRow(114557, "Alexandra", false)));	//DEBUG
	}
 	
	@Override
	public void setListeners() {
		this.itFindSomebody.addTextChangedListener(findSomebodyTextWatcher);
		this.ivSettings.setOnClickListener(onSettingsClickListener);
		this.tvBack.setOnTouchListener(onBackTouchListener);
		this.tvSend.setOnTouchListener(onSendTouchListener);
 	}
	
	
	  ///////////////
	 // LISTENERS //
	///////////////
	
	private TextWatcher findSomebodyTextWatcher = new TextWatcher() {
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			Log.i(TAG, "findSomebodyTextWatcher.beforeTextChanged");
			//TODO action before "find somebody" text changed
		}
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			Log.i(TAG, "findSomebodyTextWatcher.onTextChanged");
			Log.d(TAG, "new char sequence = "+s);
			//TODO action on "find somebody" text changed
		}
		@Override
		public void afterTextChanged(Editable s) {
			Log.i(TAG, "findSomebodyTextWatcher.afterTextChanged");
			//TODO action after "find somebody" text changed
		}
	};
 	
 	private OnClickListener onSettingsClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Log.i(TAG, "onSettingsClickListener");
			fragmentController.switchContent(SettingsFragment.newInstance());
		}
	};
	
	private OnTouchListener onBackTouchListener = new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			Log.i(TAG, "onBackTouchListener");
			tvBack.setOnTouchListener(null); //stand-by listener to prevent multi opening
			fragmentController.switchContent(HomeFragment.newInstance());
			return false;
		}
 	};
 	
	private OnTouchListener onSendTouchListener = new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			Log.i(TAG, "onSendTouchListener");
			tvSend.setOnTouchListener(null); //stand-by listener to prevent multi opening
			//TODO action on "Send" textview touched
			handler.postDelayed(delayAfterTouchCallback, 200);
			return false;
		}
 	};
	
 	private Runnable delayAfterTouchCallback = new Runnable() {
 		@Override 
 		public void run() {
 			tvSend.setOnTouchListener(onSendTouchListener);
 		}
 	};
 	
 	private OnClickListener onRowClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
 			Log.i(TAG, "onRowClickListener");
 			//TODO action on "row" view clicked
		}
 	};
 	
 	
	  /////////////////
	 // INNER CLASS //
	/////////////////
	
	private class ContactRow {
		public int id;
		public String name;
		public boolean checked;
		public ContactRow(int id, String name, boolean checked) {
			this.id = id;
			this.name = name;
			this.checked = checked;
		}
	}
	
	private View createRowForContact(ContactRow contactRow) {
		// inflate row layout
		LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View row = inflater.inflate(R.layout.row_contact, null);
		// fetch row views
		TextView tvName = (TextView) row.findViewById(R.id.textView_contact_name);
		CheckBox cbContact = (CheckBox) row.findViewById(R.id.checkBox_contact);
		// change views id to prevent multiple views with same id (e.g. listener)
		row.setId(row.getId()+contactRow.id);
		tvName.setId(tvName.getId()+contactRow.id);
		cbContact.setId(cbContact.getId()+contactRow.id);
		// fill row views
		tvName.setText(contactRow.name);
		cbContact.setChecked(contactRow.checked);
		// set listeners
		row.setOnClickListener(onRowClickListener);
		return row;
	}
	
	private View createSeparator() {
		LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View separator = inflater.inflate(R.layout.layout_separator, null);
		return separator;
	}
	
}
